package com.woniuxy.yogaapp.service.imp;

import com.woniuxy.yogaapp.dto.Check;
import com.woniuxy.yogaapp.dto.EvaluateDto;
import com.woniuxy.yogaapp.mapper.OrderMapper;
import com.woniuxy.yogaapp.mapper.UserMapper;
import com.woniuxy.yogaapp.pojo.*;
import com.woniuxy.yogaapp.service.OrderService;
import com.woniuxy.yogaapp.util.SMSUtil;
import com.woniuxy.yogaapp.util.TimeUtil;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.TimerTask;

/*
 *@author：源
 *@date:11:38:52
 *@descripion
 */
@Data
@Service
@Transactional
public class OrderServiceImp implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    private static int mo=0;
    @Override
    public String createOrder(OrderClass orderClass) {
        String result="插入失败";
        int i1;
        //先通过sid和cid把sname和cname付给orderClass
         Coach coach=orderMapper.seletCtelById(orderClass);
        orderClass.setCname(coach.getCname());
        Student student=orderMapper.seletSnameById(orderClass);
        orderClass.setSname(student.getSname());
        //1.插入sid，cid,等并返回一个oid用来插入小订单
        System.out.println("!!!!!!!");
        System.out.println(orderClass);
        System.out.println("!!!!!!!");
         orderMapper.insertOderClass(orderClass);
        /*System.out.println("---------");
        System.out.println(orderClass);*/
       /* //在这将oid插入小订单中以及
        int h=orderMapper.insertoid(orderClass);*/
        //2.创建详情小订单，并插入状态,及oid
       i1= orderMapper.insertDetailOrder(orderClass);

       if(i1>0){

           //通知教练
           //先查教练的电话号码，在和cid一起传入工具类
           Coach coach1 =orderMapper.seletCtelById(orderClass);
           /*System.out.println("***********");
           System.out.println(coach.getCnickname()+":"+coach.getCtel());
           System.out.println("***********");*/
           String [] s={coach1.getCnickname()};
           SMSUtil.appointToCoach(coach1.getCtel(),s);
           //将信息存入数据库

           Chat  chat=new Chat();
           chat.setRecipient(userMapper.selectUidBycid(new EvaluateDto(orderClass.getSid(),orderClass.getCid())).getUid());
           chat.setSender(userMapper.selectUidBysid(new EvaluateDto(orderClass.getSid(),orderClass.getCid())).getUid());
           chat.setState("未读");
           chat.setMestype(9);
           chat.setMestime(TimeUtil.getNowDate());
           chat.setMes("你有一条学员请求信息，请到我的私约中查看并处理");
           System.out.println("--------");
           System.out.println(chat);
           int i=orderMapper.insertChat(chat);
           if(i>0){
               result="插入成功";
           }

       }

        return result;
    }
    /*
    * 1.通过传入sid，did找到uid在找到pockek，减少支付用户的余额（金额必须够），（输入支付密码）用uid查密码
     *
     * 插入支付时间
     * 2.相应对应数量的金额在平台的钱包上加上
     * 3.通知教练，学员已经完成订单支付
     * 4.等待1天后 ，钱转入cid教练并再次通知教练
     * 5.更改订单状态*/
    @Override
    public String payOrder(OrderClass orderClass) {
        String result="支付完成";
         mo=orderClass.getMoney();
        //1.通过传入sid，did找到uid在找到pockek，减少支付用户的余额（金额必须够），（输入支付密码）用uid查密码
        OrderClass c= orderMapper.selectMoney(orderClass);
        System.out.println(c);
        System.out.println(c.getMoney());

        if(c.getMoney()>=orderClass.getMoney()){
            System.out.println("金额足够可进行下一步");
            //2.用户的钱包金额减少,更新余额
            orderClass.setMoney(c.getMoney()-orderClass.getMoney());
            int i=orderMapper.updateMoney(orderClass);


        }
        else{
        	return "金额不足够，请先充值";
        }
        //3.插入支付时间,更改状态已支付
        orderClass.getCurrentDetailOrders().setPaytime(TimeUtil.getNowDate());
        orderClass.getCurrentDetailOrders().setState(5);
        int j =orderMapper.updatePayTime(orderClass);
        System.out.println("时间已经更新成功");

        //4.更改平台的钱包（先查询余额加了在刷新）3平台id
        OrderClass q= orderMapper.selectAdminMoney();
        System.out.println(q.getMoney());
        System.out.println(q.getMoney()+mo);
        orderClass.setMoney(q.getMoney()+mo);

       int s= orderMapper.updateAdminMoney(orderClass);
        System.out.println("管理员钱已经加入可 进行下一步");
        //5.通知教练
        Chat  chat=new Chat();
        chat.setRecipient(userMapper.selectUidBycid(new EvaluateDto(orderClass.getSid(),orderClass.getCid())).getUid());
        chat.setSender(userMapper.selectUidBysid(new EvaluateDto(orderClass.getSid(),orderClass.getCid())).getUid());
        chat.setMestype(9);
        chat.setState("未读");
        chat.setMestime(TimeUtil.getNowDate());
        chat.setMes("订单已经付款,1天后到账，请注意查收");

        System.out.println(chat);
        int i=orderMapper.insertChat(chat);
        System.out.println("-------------------------------");
        System.out.println(orderClass);
        //6.发送短信给教练

        //7.定时一段时间转让教练的钱包，
        new java.util.Timer().schedule(new TimerTask(){
            public void run() {
                //这里写延迟后要运行的代码
                System.out.println(update(orderClass));
                this.cancel();
            }}, 10000);
        System.out.println("支付完成");
        return result;
    }

    @Override
    public String selectishave(OrderClass orderClass) {


        DetailOrder d = new DetailOrder();
        d.setState(1);
        orderClass.setCurrentDetailOrders(d);

        Check databaseorderClass=orderMapper.selectishave(orderClass);
        System.out.println(databaseorderClass);
        if(databaseorderClass==null){
            return "没有未处理订单";
        }
        //如果有小订单没有处理，那么覆盖新的大订单，但不会给教练发送通知，只是更改通知的内容
        int i=orderMapper.reupdate(orderClass);


        return "有未处理订单，已经覆盖";
    }

    public String update(OrderClass orderClass){
        System.out.println(orderClass+"!!!!!!!!!!!");
        OrderClass c1=orderMapper.selectMoneyC(orderClass);
        int Cmoney =c1.getMoney();
        System.out.println(Cmoney);
        orderClass.setMoney(Cmoney+mo);
        int x=orderMapper.updateMoneyC(orderClass);
        System.out.println("教练的钱已经到账可进行下一步");
        //平台减少，
        c1= orderMapper.selectAdminMoney();
        orderClass.setMoney(c1.getMoney()-mo);
        int aMoney=orderMapper.updateAdminMoney(orderClass);
        //更新状态
        orderClass.getCurrentDetailOrders().setState(6);
        orderClass.getCurrentDetailOrders().setCompletime(TimeUtil.getNowDate());
        int l=orderMapper.updateCTime(orderClass);
        //再次通知教练

        System.out.println("订单");
            return "订单完成";

    }

}
